﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;
using System.IO.Ports;
using System.Xml;

namespace KeaoMDS.SysManage
{
    public partial class SysConfig : BaseForm
    {
        public SysConfig()
        {
            InitializeComponent();
        }

        private void menu_btn_guanbi_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void menu_btn_baocun_Click(object sender, EventArgs e)
        {
            //保存
            KeaoUtils.ConfigSetNodeAttr("视频监控", "登录名", txt_shipin_dengluming.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "登录密码", KeaoUtils.Encrypt(txt_shipin_denglumima.Text));
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号1", txt_shipin_tongdao1.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号2", txt_shipin_tongdao2.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号3", txt_shipin_tongdao3.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号4", txt_shipin_tongdao4.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号5", txt_shipin_tongdao5.Text);
            KeaoUtils.ConfigSetNodeAttr("视频监控", "通道号6", txt_shipin_tongdao6.Text);

            KeaoUtils.ConfigSetNodeAttr("视频监控", "状态", chk_shipin_zhuangtai.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("称重设备", "波特率", txt_chengzhong_botelv.Text);
            KeaoUtils.ConfigSetNodeAttr("称重设备", "状态", chk_chengzhong_yibiaozhuangtai.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("称重设备", "间隔标识", KeaoUtils.Encrypt(txt_chengzhong_yibiaojiange.Text));
            KeaoUtils.ConfigSetNodeAttr("遥控监测设备", "状态", chk_yaokong_zhuangtai.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("系统配置", "皮重差", txt_xitong_pizhongcha.Text);
            KeaoUtils.ConfigSetNodeAttr("系统配置", "称重超时", txt_xitong_chengzhongchaoshi.Text);
            KeaoUtils.ConfigSetNodeAttr("系统配置", "车辆运输次数预警", txt_xitong_cishuyujing.Text);
            KeaoUtils.ConfigSetNodeAttr("网络设备", "状态", chk_dtu.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("网络设备", "上传图片", chk_tupian.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("客户", "项目部", txt_xitong_xiangmubu.Text);
            KeaoUtils.ConfigSetNodeAttr("客户", "磅房", txt_bangfang.Text);
            KeaoUtils.ConfigSetNodeAttr("显示设备", "状态", chk_xianshishebei.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("时段", "月结", txt_yuejie.Text);
            KeaoUtils.ConfigSetNodeAttr("时段", "日结提醒", chk_rijie.Checked ? "启用" : "禁用");
            KeaoUtils.ConfigSetNodeAttr("时段", "提醒时间", txt_tixingshijian.Text);
            KeaoUtils.ConfigSetNodeAttr("系统配置", "出库称重超时", txt_xitong_chengzhongchaoshi_chuku.Text);
            KeaoUtils.ConfigSetNodeAttr("系统配置", "出库车辆运输次数预警", txt_xitong_cishuyujing_chuku.Text);
            KeaoUtils.ConfigRemoveAllChildNodes("网络设备");
            foreach (string str in lst_shoujihaoma.Items)
            {
                KeaoUtils.ConfigAddChildNodesInnerText("网络设备", "发送号码", str);
            }
            

            KeaoUtils.ShowInfoMessage("设置参数完成。");
        }

        private void SysConfig_Load(object sender, EventArgs e)
        {
            if (KeaoUtils.ConfigGetNodeAttr("网络设备", "类型") == "KeaoNetwork")
            {
                lst_shoujihaoma.Enabled = false;
                txt_shoujihaoma.Enabled = false;
                btn_shanchushoujihaoma.Enabled = false;
                btn_tianjiashoujihao.Enabled = false;
                lst_shoujihaoma.Items.Clear();
            }
            else
            {
                XmlNodeList shouJiHao = KeaoUtils.ConfigGetNodeListByTag("发送号码");
                foreach (XmlNode node in shouJiHao)
                {
                    lst_shoujihaoma.Items.Add(node.InnerText);
                }
            }
           chk_tupian.Checked = KeaoUtils.ConfigGetNodeAttr("网络设备", "上传图片") == "启用" ? true : false;
           chk_dtu.Checked = KeaoUtils.ConfigGetNodeAttr("网络设备", "状态") == "启用" ? true : false;
           txt_shipin_dengluming.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "登录名");
           txt_shipin_denglumima.Text =KeaoUtils.Decrypt(KeaoUtils.ConfigGetNodeAttr("视频监控", "登录密码"));
           txt_shipin_tongdao1.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号1");
           txt_shipin_tongdao2.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号2");
           txt_shipin_tongdao3.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号3");
           txt_shipin_tongdao4.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号4");
           txt_shipin_tongdao5.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号5");
           txt_shipin_tongdao6.Text = KeaoUtils.ConfigGetNodeAttr("视频监控", "通道号6");
           chk_shipin_zhuangtai.Checked = KeaoUtils.ConfigGetNodeAttr("视频监控", "状态").Equals("启用") ? true : false;
           txt_chengzhong_botelv.Text = KeaoUtils.ConfigGetNodeAttr("称重设备", "波特率");
           chk_chengzhong_yibiaozhuangtai.Checked = KeaoUtils.ConfigGetNodeAttr("称重设备", "状态") == "启用" ? true : false;
           txt_chengzhong_yibiaojiange.Text = KeaoUtils.Decrypt(KeaoUtils.ConfigGetNodeAttr("称重设备", "间隔标识"));
           chk_yaokong_zhuangtai.Checked = KeaoUtils.ConfigGetNodeAttr("遥控监测设备", "状态") == "启用" ? true : false;
           txt_xitong_pizhongcha.Text = KeaoUtils.ConfigGetNodeAttr("系统配置", "皮重差");
           txt_xitong_cishuyujing.Text = KeaoUtils.ConfigGetNodeAttr("系统配置", "车辆运输次数预警");
           txt_xitong_chengzhongchaoshi.Text = KeaoUtils.ConfigGetNodeAttr("系统配置", "称重超时");

           txt_xitong_xiangmubu.Text = KeaoUtils.ConfigGetNodeAttr("客户", "项目部");
           txt_bangfang.Text = KeaoUtils.ConfigGetNodeAttr("客户", "磅房");
           chk_xianshishebei.Checked = KeaoUtils.ConfigGetNodeAttr("显示设备", "状态") == "启用" ? true : false;
           txt_yuejie.Text = KeaoUtils.ConfigGetNodeAttr("时段", "月结");
           chk_rijie.Checked = KeaoUtils.ConfigGetNodeAttr("时段", "日结提醒") == "启用" ? true : false;
           txt_tixingshijian.Text = KeaoUtils.ConfigGetNodeAttr("时段", "提醒时间");
           txt_xitong_chengzhongchaoshi_chuku.Text = KeaoUtils.ConfigGetNodeAttr("系统配置", "出库称重超时");
           txt_xitong_cishuyujing_chuku.Text = KeaoUtils.ConfigGetNodeAttr("系统配置", "出库车辆运输次数预警");
           
        }

        private void btn_shanchushoujihaoma_Click(object sender, EventArgs e)
        {
            if (lst_shoujihaoma.SelectedIndex >= 0)
            {
                lst_shoujihaoma.Items.RemoveAt(lst_shoujihaoma.SelectedIndex);
            }
        }

        private void btn_tianjiashoujihao_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(txt_shoujihaoma.Text)&&KeaoUtils.isPhoneNumber(txt_shoujihaoma.Text))
            {
                lst_shoujihaoma.Items.Remove(txt_shoujihaoma.Text);
                lst_shoujihaoma.Items.Add(txt_shoujihaoma.Text);
                txt_shoujihaoma.Text = string.Empty;
            }
        }

    }
}
